package com.sun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.enums.HttpEnum;
import com.sun.mapper.PurchaseSettlementMapper;
import com.sun.pojo.entity.PurchaseOrders;
import com.sun.pojo.entity.PurchaseSettlement;
import com.sun.pojo.vo.R;
import com.sun.service.PurchaseOrdersService;
import com.sun.service.PurchaseSettlementService;
import com.sun.utils.DingDingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 采购结算表(PurchaseSettlement)表服务实现类
 *
 * @author makejava
 * @since 2024-10-25 08:18:16
 */
@Service("purchaseSettlementService")
public class PurchaseSettlementServiceImpl implements PurchaseSettlementService {
    @Autowired
    PurchaseSettlementMapper purchaseSettlementMapper;
    @Autowired
    PurchaseOrdersService purchaseOrdersService;
    /**
     * 通过ID查询单条数据
     *
     * @param psId 主键
     * @return 实例对象
     */
    @Override
    public PurchaseSettlement queryById(Integer psId) {
        return purchaseSettlementMapper.queryById(psId);
    }

    /**
     * 分页查询
     *
     * @param purchaseSettlement 筛选条件
     * @param pageNum            分页对象
     * @return 查询结果
     */
    @Override
    public PageInfo<PurchaseSettlement> queryByPage(PurchaseSettlement purchaseSettlement, Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<PurchaseSettlement> list = purchaseSettlementMapper.queryAllByLimit(purchaseSettlement);
        PageInfo<PurchaseSettlement> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param purchaseSettlement 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(PurchaseSettlement purchaseSettlement) {
        if (purchaseSettlementMapper.insert(purchaseSettlement) > 0) {
            return 1;
        }
        return -1;
    }

    /**
     * 修改数据
     *
     * @param purchaseSettlement 实例对象
     * @return 实例对象
     */
    @Override
    public R update(PurchaseSettlement purchaseSettlement) {
        // 修改订购记录为已收货
        String orderId = purchaseSettlement.getPoId();
        PurchaseOrders purchaseOrders = new PurchaseOrders();
        DingDingUtil dingDingUtil = new DingDingUtil();
        dingDingUtil.getDingDingText("订单:"+orderId+"已收货!");
        purchaseOrders.setOrderId(orderId);
        purchaseOrders.setStatus(2);
        if (purchaseOrdersService.update(purchaseOrders).getCode() == 200) {
            purchaseSettlement.setSettlementStatus("1");
            if (purchaseSettlementMapper.update(purchaseSettlement) > 0) {
                return R.success("修改成功");
            }
        }
        return R.fail(HttpEnum.WARN.getCode(), "修改失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param poId 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(Integer poId) {
        if (purchaseSettlementMapper.deleteById(poId) > 0) {
            return 1;
        }
        return -1;
    }
}
